package com.chenzehe.java.core.kaptcha;

import com.chenzehe.java.core.SpringContextHolder;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class KcaptcherServlet extends HttpServlet {
	
	private Producer captchaProducer = null; 

	private final static String COOKIE_NAME = "_reg_key_"; 

	/**
	 * Constructor of the object.
	 */
	public KcaptcherServlet() {
		super();
	}
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init(ServletConfig servletConfig) throws ServletException {
		captchaProducer=SpringContextHolder.getBean("captchaProducer");

	}
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		// Set to expire far in the past.
				response.setDateHeader("Expires", 0);
				// Set standard HTTP/1.1 no-cache headers.
				response.setHeader("Cache-Control","no-store, no-cache, must-revalidate");
				// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
				response.addHeader("Cache-Control", "post-check=0, pre-check=0");
				// Set standard HTTP/1.0 no-cache header.
				response.setHeader("Pragma", "no-cache");

				// return a jpeg
				response.setContentType("image/jpeg");

				// create the text for the image
				String capText = captchaProducer.createText();

				// store the text in the session
				HttpSession session = request.getSession();
				session.setAttribute(Constants.KAPTCHA_SESSION_KEY,capText);

				// create the image with the text
				BufferedImage bi = captchaProducer.createImage(capText);

				ServletOutputStream out = response.getOutputStream();

				// write the data out
				ImageIO.write(bi, "jpg", out);
				try {
					out.flush();
				} finally {
					out.close();
				}
				
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
			throws ServletException, IOException {

		doGet(httpServletRequest, httpServletResponse);
	}


	
	

	

}
